package com.white.mybatis.dynamic;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mysql关键字
 * </p>
 *
 * @author huangzy
 * @since 2022/2/22
 */
public enum MysqlKeywords implements SqlPrefixStatementBuildr {


    /**
     * 表结构查询
     */
    DESCRIBE("SHOW FULL COLUMNS FROM") {
        @Override
        public String build(String tableName, List<TableFieldValue> entityValues) {
            return this.getKeywords() + " " + tableName;
        }
    },

    /**
     * 查询语句
     */
    SELECT("SELECT") {
        /**
         * 针对于对表连接查询Sql拼接
         * @param master 主表
         * @param entityValues 表与字段的映射
         * @return
         */
        @Override
        public String build(String master , Map<String,List<TableFieldValue>> entityValues){
            if (entityValues == null || entityValues.isEmpty()) {
                return this.getKeywords() + " * FROM " + master + " ";
            }
            StringBuilder sb = new StringBuilder(this.getKeywords()).append(" ");
            //拼接查询字段
            entityValues.forEach((key,value) -> {
                splicColumn(key,value,sb);
                sb.append(",");
            });
            sb.replace(sb.length()-1,sb.length(),"");
            sb.append(" FROM ");
            sb.append(master);
            sb.append(" ");
            return sb.toString();
        }

        /**
         * 拼接查询字段
         * @param tableName 表名
         * @param entityValues 字段
         * @param sb 被拼接的字符串
         */
        private void splicColumn(String tableName, List<TableFieldValue> entityValues , StringBuilder sb){
            for (int i = 0; i < entityValues.size(); i++) {
                TableFieldValue item = entityValues.get(i);
                sb.append(item.getTableName());
                sb.append(".");
                sb.append(item.getColumn());
                sb.append(" AS ");
                sb.append(item.getPropertyView());
                sb.append(" ");
                if (i < entityValues.size() - 1) {
                    sb.append(" , ");
                }
            }
        }

        @Override
        public String build(String tableName, List<TableFieldValue> entityValues) {
            if (entityValues == null || entityValues.isEmpty()) {
                return this.getKeywords() + " * FROM " + tableName + " ";
            }
            StringBuilder sb = new StringBuilder(this.getKeywords()).append(" ");
            //拼接查询字段
            splicColumn(tableName,entityValues,sb);
            sb.append(" FROM ");
            sb.append(tableName);
            sb.append(" ");
            return sb.toString();
        }
    },

    /**
     * 删除语句
     */
    DELETE("DELETE") {
        @Override
        public String build(String tableName, List<TableFieldValue> entityValues) {
            return this.getKeywords() + " FROM " + tableName + " ";
        }
    },

    /**
     * 更新语句
     */
    UPDATE("UPDATE") {
        @Override
        public String build(String tableName, List<TableFieldValue> entityValues) {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getKeywords());
            sb.append(" ");
            sb.append(tableName);
            sb.append(" ");
            sb.append(" SET ");
            for (int i = 0; i < entityValues.size(); i++) {
                TableFieldValue item = entityValues.get(i);
                sb.append(item.getColumn());
                sb.append(" = ? ");
                if (i < entityValues.size() - 1) {
                    sb.append(" , ");
                }
            }
            return sb.toString();
        }
    },

    /**
     * 更新语句
     */
    INSERT("INSERT") {
        @Override
        public String build(String tableName, List<TableFieldValue> entityValues) {
            StringBuilder sb = new StringBuilder();
            sb.append(this.getKeywords());
            sb.append(" INTO ");
            sb.append(tableName);
            sb.append("(");
            for (int i = 0; i < entityValues.size(); i++) {
                if (i > 0) {
                    sb.append(",");
                }
                sb.append(entityValues.get(i).getColumn());
            }
            sb.append(") VALUE (");
            for (int i = 0; i < entityValues.size(); i++) {
                if (i > 0) {
                    sb.append(",");
                }
                sb.append("?");
            }
            sb.append(")");
            return sb.toString();
        }
    },
    ;

    /**
     * SQL 关键字
     */
    private final String keywords;

    MysqlKeywords(String keywords) {
        this.keywords = keywords;
    }

    public String getKeywords() {
        return keywords;
    }
}
